﻿using System;
using System.IO;
using System.Diagnostics;
using CommandLine;
using CommandLine.Text;

namespace SPSource
{
    class Program
    {
        private static readonly HeadingInfo _headingInfo = new HeadingInfo("SPSource", "1.1.1.0");

        static void Main(string[] args)
        {
            //Debugger.Launch();
            try
            {
                var options = new Options();
                ICommandLineParser parser = new CommandLineParser();
                if (parser.ParseArguments(args, options, Console.Error))
                {
                    string path = Environment.CurrentDirectory;
                    var currentDirectory = new DirectoryInfo(Environment.CurrentDirectory);

                    var spSource = new SPSource();
                    spSource.DesignSiteUrl = options.DesignSite;
                    spSource.OverrideFeatureDirectoryCheck = (options.OverrideFeatureDirectoryCheck == "1" ? true : false);
                    spSource.RootProjDir = currentDirectory;
                    spSource.Execute();

                    Utility.WriteMessage("SPSource Completed", "Completed");
                }
                else
                {
                    Environment.Exit(1);
                }
            }
            catch (Exception ex)
            {
                Utility.WriteMessage("An exception occured while running SPSource:" + ex.Message, "Exception occured");
            }
        }

        private sealed class Options
        {
            [Option("d", "DesignSite",
                    Required = true,
                    HelpText = "Design site used to reverse engineer artefacts.")]
            public string DesignSite = String.Empty;
            [Option("o", "OverrideFeatureDirectoryCheck",
                    Required = false,
                    HelpText = "Override check for features directory (1|0)")]
            public string OverrideFeatureDirectoryCheck = false.ToString();

            [HelpOption(HelpText = "Display this help screen.")]
            public string GetUsage()
            {
                HelpText help = new HelpText(Program._headingInfo);
                help.Copyright = new CopyrightInfo("@Jthake & @RichFinn", 2008, 2009);
                help.AddPreOptionsLine("Usage: SPSource -d http://server/devsite");
                help.AddPreOptionsLine("Usage: SPSource --DesignSite http://server/devsite");
                help.AddPreOptionsLine("Usage: SPSource --DesignSite http://server/devsite --OverrideFeatureDirectoryCheck true");
                help.AddOptions(this);
                return help;
            }
        }

    }
}
